package com.vvv.designpattern.ordermanagement.command.receiver;

import com.vvv.designpattern.ordermanagement.audit.*;
import com.vvv.designpattern.ordermanagement.state.OrderStateChangeAction;
import com.vvv.designpattern.pojo.Order;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author vvv
 * @date 2023-11-22 08 45
 * discription命令模式：接受者角色：真正执行命令的角色，订单转换流程的相关逻辑在这里实现
 * 接受者角色可以有多个，比如说专门用来处理ORDER_WAIT_PAY的receiver等等
 */
@Component
public class OrderCommandReceiver {

    @Resource
    private CreateOrderLog createOrderLog;

    @Resource
    private PayOrderLog payOrderLog;

    @Resource
    private SendOrderLog sendOrderLog;

    @Resource
    private ReceiveOrderLog receiveOrderLog;

    //mock
    public static final int userId = 1;

    public void action(Order order) {
        switch (order.getOrderState()) {
            case ORDER_WAIT_PAY:
                System.out.println("创建订单：" + order);
                System.out.println("存入DB");
                // 日志
                OrderAuditLog createOrderLogAuditLog = createOrderLog.createAuditLog(userId, "创建订单", order.getOrderId());
                System.out.println("将创建订单日志发送到mq中：" + createOrderLogAuditLog);
                return;
            case ORDER_WAIT_SEND:
                System.out.println("支付订单：" + order);
                System.out.println("存入DB");
                System.out.println("通过mq 通知财务部门");
                System.out.println("通过mq 通知发货部门");
                // 日志
                Map<String, Object> orderMap = new HashMap<>();
                orderMap.put("payType", "支付宝");
                orderMap.put("price", "100");
                OrderAuditLog payOrderLogAuditLog = payOrderLog.createAuditLog(userId, "支付订单", order.getOrderId(), orderMap);
                System.out.println("将支付订单日志发送到mq中：" + payOrderLogAuditLog);
                return;
            case ORDER_WAIT_RECEIVE:
                System.out.println("订单发货：" + order);
                System.out.println("存入DB");
                Map<String, Object> sendMap = new HashMap<>();
                sendMap.put("快递公司", "x速递");
                sendMap.put("快递编号", "100100100100");
                OrderAuditLog sendOrderLogAuditLog = sendOrderLog.createAuditLog(userId, "发送订单", order.getOrderId(), sendMap);
                System.out.println("将发货日志发送到mq中：" + sendOrderLogAuditLog);
                return;
            case ORDER_FINISH:
                System.out.println("接收订单：" + order);
                System.out.println("存入DB");
                OrderAuditLog receiveOrderLogAuditLog = receiveOrderLog.createAuditLog(userId, "接收订单", order.getOrderId());
                System.out.println("将收获日志发送到mq中：" + receiveOrderLogAuditLog);
                return;
            default:
                throw new UnsupportedOperationException("order state error !");
        }
    }
}
